<?php
namespace system\platform\controller;

class Wechat{

    // 微信公众号设置
    function wxweb(){

        if( isset($_POST['name']) && $_POST['name'] !='' ){ 

            $data_tmp['uniacid'] = 0;
            $data_tmp['name'] = trim($_POST['name']);
            $data_tmp['appid'] = trim($_POST['appid']);
            $data_tmp['appsecret'] = trim($_POST['appsecret']);
            $data_tmp['token'] = trim($_POST['token']);
            $data_tmp['aes_key'] = trim($_POST['aes_key']);

            //
            $data= [ 'value'=> json_encode($data_tmp) ,'update_time'=> date('Y-m-d H:i:s') ]; 

            $where=['name'=>'微信公众号'];
            
            $ok = pdo_update('sys_config',$data,$where);    // 修改设置
            if($ok){
                message('修改成功',url(['platform','wechat','wxweb']));
            }else{
                message('修改失败,请重试!',url(['platform','wechat','wxweb']));
            }


        }else{

            if( isset($GLOBALS['config']['微信公众号']) ){
                $ret_config = $GLOBALS['config']['微信公众号'];
            }else{
                $ret_config = ['uniacid'=>'0','name'=>'微信公众号','appid'=>'','appsecret'=>'','token'=>'','aes_key'=>''];
                $data=['type'=>'wechat', 'name'=>'微信公众号' ,'value'=> json_encode($ret_config) , 'description'=>'微信公众号认证服务号配置', 'update_time'=> date('Y-m-d H:i:s') ];
                pdo_insert('sys_config',$data);
            }
            

            require view('default/wechat/wxweb.tpl.php');
        }

    } 

    // 微信小程序
    function wxapp(){
        if( isset($_POST['name']) && $_POST['name'] !='' ){ 

            $data_tmp['uniacid'] = 0;
            $data_tmp['name'] = trim($_POST['name']);
            $data_tmp['appid'] = trim($_POST['appid']);
            $data_tmp['appsecret'] = trim($_POST['appsecret']);

            $data= [ 'value'=> json_encode($data_tmp) ,'update_time'=> date('Y-m-d H:i:s') ]; 

            $where=['name'=>'微信小程序'];
            $ok = pdo_update('sys_config',$data,$where);    // 修改设置
            if($ok){
                message('修改成功',url(['platform','wechat','wxapp']));
            }else{
                message('修改失败,请重试!',url(['platform','wechat','wxapp']));
            }
            
        }else{

            if( isset($GLOBALS['config']['微信小程序']) ){
                $ret_config = $GLOBALS['config']['微信小程序'];
            }else{
                $ret_config = ['uniacid'=>'0','name'=>'微信小程序','appid'=>'','appsecret'=>''];
                $data=['type'=>'wechat', 'name'=>'微信小程序' ,'value'=> json_encode($ret_config) , 'description'=>'微信小程序配置', 'update_time'=> date('Y-m-d H:i:s') ];
                pdo_insert('sys_config',$data);
            }
            require view('default/wechat/wxapp.tpl.php');
        }

    }

    // 微信支付设置
    function pay(){

        //微信支付API证书
        $file_path = _APP_PATH_ . '/data/pay/wechat/cert';  // 证书目录
        // 目录不存在则自动创建
        if (!is_dir($file_path)) {
            mkdir( $file_path, 0755, true );
        }
        if( !file_exists( $file_path . '/apiclient_cert.pem')){
            file_put_contents( $file_path . '/apiclient_cert.pem','' );
        }
        if( !file_exists( $file_path . '/apiclient_key.pem')){
            file_put_contents( $file_path . '/apiclient_key.pem','' );
        }

        //
        if( isset($_POST['mch_id']) && $_POST['mch_id'] !='' ){

            //微信支付API证书 写
            $apiclient_cert_pem = file_put_contents( $file_path . '/apiclient_cert.pem', trim($_POST['apiclient_cert_pem']) );
            $apiclient_key_pem = file_put_contents( $file_path . '/apiclient_key.pem',trim($_POST['apiclient_key_pem']) );

            $data_tmp['uniacid'] = 0;
            $data_tmp['mch_id'] = trim($_POST['mch_id']);
            $data_tmp['apikey'] = trim($_POST['apikey']);

            $data= [ 'value'=> json_encode($data_tmp) ,'update_time'=> date('Y-m-d H:i:s') ]; 

            $where=['name'=>'微信支付'];
            $ok = pdo_update('sys_config',$data,$where);    // 修改设置
            
            if($ok){
                message('修改成功',url(['platform','wechat','pay']));
            }else{
                message('修改失败,请重试!',url(['platform','wechat','pay']));
            }

        }else{

            if( isset($GLOBALS['config']['微信支付']) ){
                $ret_config = $GLOBALS['config']['微信支付'];
            }else{
                $ret_config = ['uniacid'=>'0','mch_id'=>'','apikey'=>''];
                $data=['type'=>'wechat', 'name'=>'微信支付' ,'value'=> json_encode($ret_config) , 'description'=>'微信支付配置', 'update_time'=> date('Y-m-d H:i:s') ];
                pdo_insert('sys_config',$data);
            }
    
            //微信支付API证书 读
            $apiclient_cert_pem = file_get_contents( $file_path . '/apiclient_cert.pem');
            $apiclient_key_pem =  file_get_contents( $file_path . '/apiclient_key.pem');

            require view('default/wechat/pay.tpl.php');
        }
    }

    // 菜单设置
    function menu(){
        //error_reporting(0);
        try{
            $menu_list = $GLOBALS['wechat']['app']->menu->list();
            if( isset($menu_list['errcode']) && $menu_list['errcode']!=0){
                $menu_list['menu'] = array() ;
            }
        }catch(\Exception $e){
            $menu_list['menu'] = array() ;
        }
        require view('default/wechat/menu.tpl.php');
    }

    //添加菜单
    function menu_save(){
        if(isset($_POST['menu']) && $_POST['menu']!=''){
            //数据转换为数组
            $menu = $this->object_to_array( json_decode($_POST['menu']) );
            //判断是否菜单全部删除
            if(empty($menu['button'])){
                //清空菜单
                $ok = $GLOBALS['wechat']['app']->menu->delete();
            }else{
                //添加菜单
                $ok = $GLOBALS['wechat']['app']->menu->create( $menu['button'] );
            }
            if($ok['errcode']!=0){
                $data['code']=0;
                $data['msg']='修改失败,请重试!';
            }else{
                $data['code']=1;
                $data['msg']='修改成功!';
            }
        }else{
            $data['code']=0;
            $data['msg']='修改失败,请重试!';
        }
        //返回数据
        echo json_encode($data);
    }

    //对象转数组
    function object_to_array($obj) {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)$this->object_to_array($v);
            }
        }
        return $obj;
    }

}